package com.sunday.pinxinghui.model.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.pinxinghui.MainActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.network.ApiClient;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/11.
 */

public class LoginActivity extends BaseActivity {


    private UMShareAPI umShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        umShareAPI = UMShareAPI.get(this);
        umShareAPI.deleteOauth(LoginActivity.this, SHARE_MEDIA.WEIXIN, null);

    }

    @OnClick({R.id.mobile_login, R.id.wechat_login, R.id.btn_register})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.mobile_login:
                intent = new Intent(mContext, MobileLoginActivity.class);
                startActivity(intent);
                break;
            case R.id.wechat_login:
                umShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, umAuthInfoListener);
                break;
            case R.id.btn_register:
                intent = new Intent(mContext, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    private String logo, nickName, unionid, openid;
    private UMAuthListener umAuthInfoListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            switch (share_media) {
                case WEIXIN:
                    openid = map.get("openid");
                    logo = map.get("iconurl");
                    nickName = map.get("name");
                    unionid = map.get("unionid");
                    thirdLogin();
                    break;
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            ToastUtils.showToast(mContext, "授权失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            ToastUtils.showToast(mContext, "授权取消");
        }
    };


    private void thirdLogin() {
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().login(2, null, null, null, unionid, nickName, null, logo);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (response.body().getCode() == 0) {
                    if (resultDO.getResult() != null && !TextUtils.isEmpty(resultDO.getResult().getMobile())) {
                        SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                        SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                        Member member = response.body().getResult();
                        BaseApplication.getInstance().setMember(member);
                        intent = new Intent(mContext, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                        Member member = response.body().getResult();
                        BaseApplication.getInstance().setMember(member);
                        intent = new Intent(mContext, BindMobileActivity.class);
                        startActivity(intent);
                    }

                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }


}
